package com.cheer.tool;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.cheer.util.ToolUtils;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.aspectj.util.FileUtil;

import java.io.*;
import java.util.*;


public class VoteZXLJImport {

    public static void main(String[] args) throws IOException {
        // excel 数据
        // ExcelReader reader = ExcelUtil.getReader("F:\\大学习\\活动点赞\\向上向善\\向上向善汇总\\向上向善汇总表.xlsx");
        ExcelReader reader = ExcelUtil.getReader("F:\\大学习\\活动点赞\\振兴龙江\\“看见龙江”省青少年摄影大赛获奖情况表.xlsx");
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        int sheetCount = reader.getSheetCount();
        int count = 0;

        Set<String> fileType = new HashSet<>();
        for (int i = 0; i <sheetCount; i++) {
            ExcelReader sheetContent = ExcelUtil.getReader("F:\\大学习\\活动点赞\\振兴龙江\\“看见龙江”省青少年摄影大赛获奖情况表.xlsx", i);
            List<Map<String, Object>> readAll = sheetContent.readAll();

            for (int j = 0; j < readAll.size(); j++) {
                // 类别	编号	推荐人	单位
                Map<String, Object> stringObjectMap = readAll.get(j);
                String dirName = String.valueOf(stringObjectMap.get("组别"));
                String zp = String.valueOf(stringObjectMap.get("作品"));
                String jx = String.valueOf(stringObjectMap.get("奖项"));
                String recommendPeople = String.valueOf(stringObjectMap.get("姓名"));
                String zpmc = String.valueOf(stringObjectMap.get("作品名称"));
                String remark = String.valueOf(stringObjectMap.get("推荐单位"));
                if(StrUtil.isBlank(remark)){
                    continue;
                }
                String intro = "";
                String story = zpmc;
                String image = "vote7/";
                String miniimage = "vote7_mini/";
                String photo = "";

                String absolutePath = "F:\\大学习\\活动点赞\\振兴龙江\\图像汇总" + File.separator;
                // 读取文件下的图片个个人事迹
                String filePath = "F:\\大学习\\活动点赞\\振兴龙江" + File.separator + dirName + File.separator + jx + File.separator;
                String[] strings = FileUtil.listFiles(new File(filePath));
                List<Map<String, String>> lists = new ArrayList<>();
                for (int k = 0; k < strings.length; k++) {
                    if(strings[k].contains(recommendPeople)){

                        if("doc".equals(cn.hutool.core.io.FileUtil.extName(strings[k]))
                                || "docx".equals(cn.hutool.core.io.FileUtil.extName(strings[k]))
                                        || "pdf".equals(cn.hutool.core.io.FileUtil.extName(strings[k]))){
                            continue;
                        }

                        File file = new File(filePath + File.separator + strings[k]);

                        if(file.isFile()){
                            photo = md5.digestHex(strings[k]) +"."+ cn.hutool.core.io.FileUtil.extName(strings[k]);
                            Map<String, String> imagePath = new HashMap<>();
                            imagePath.put("photo", "vote7/" + photo );
                            imagePath.put("photoRectangle", "vote7_rectangle/" + photo );
                            lists.add(imagePath);
                            // 将图片移动至统一文件夹
                            FileUtil.copyFile(file ,new File(absolutePath+photo));
                        }
                    }
                }
                intro = JSONUtil.parseArray(lists).toString();
                if(CollectionUtil.isEmpty(lists)){
                    System.out.println(strings);
                    System.out.println("recommendPeople:"+ recommendPeople);
                    System.out.println( "未查询到图片记录：" + stringObjectMap);
                }
                photo = lists.get(0).get("photo").replace("vote7/", "");

                // 打印sql
                String exVotePersonSql = ToolUtils.getExVotePersonSql(1110 + count, "7", getCategoryIdByName(dirName), ToolUtils.getExVotePersonPersonNumber(String.valueOf(count + 1)), recommendPeople, "vote7_square/"+photo, remark);
                System.out.println(exVotePersonSql);
                String exVotePersonExtraSql = ToolUtils.getExVotePersonExtraSql(1110 + count, intro, story);
                System.out.println(exVotePersonExtraSql);
                count++;
            }

        }

        System.out.println("文件类型输出：" + fileType);
    }

    static String read(String file) throws Exception {
        InputStream is = new BufferedInputStream(new FileInputStream(file)); //really a OOXML Word file
        String text = "";
        if (FileMagic.valueOf(is) == FileMagic.OLE2) {
            WordExtractor ex = new WordExtractor(is);
            text = ex.getText();
            ex.close();
        } else if (FileMagic.valueOf(is) == FileMagic.OOXML) {
            XWPFDocument doc = new XWPFDocument(is);
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
            text = extractor.getText();
            extractor.close();
        }
        return text;
    }


    /**
     * 获取分类ID
     * @param dirName
     * @return
     */
    private static String getCategoryIdByName(String dirName) {

        Map<String, String> map = new HashMap<>();
        map.put("少年组","22");
        map.put("青年组","23");
        map.put("留学生组","24");
        return map.get(dirName);
    }


}
